﻿using System.Linq;
using System.Data.Linq;
using UniMana.Data;
using System;
namespace UniMana.Data
{
    public static class Training_Class_RoomData_Chi
    {
        
        /// <summary>
        /// Lấy danh sách phòng rảnh với thời gian truyền vào
        /// </summary>
        /// <param name="class_Room"></param>
        /// <param name="session_Subject_Class_Room"></param>
        /// <param name="SessionKey"></param>
        /// <param name="WeekDayKey"></param>
        /// <param name="TermKey"></param>
        /// <param name="SchoolYearKey"></param>
        /// <returns></returns>
        public static IQueryable
            GetBySubject_Class_Session(this Table<Training_Class_Room> class_Room,
            Table<Training_Session_Subject_Class_Room> session_Subject_Class_Room,
            int SessionKey,            
            int WeekDayKey,
            int TermKey,
            int SchoolYearKey,
            int NumStudent,
            DateTime RegistTime)
        {
            var IQuery = from cr in class_Room
                         where
                           !(from se_sj_cr in session_Subject_Class_Room
                              where
                                se_sj_cr.Training_SessionKey == SessionKey &&
                                se_sj_cr.Catalogue_TermKey == TermKey &&
                                se_sj_cr.RegistTime == RegistTime &&
                                se_sj_cr.Training_WeekDayKey == WeekDayKey &&
                                se_sj_cr.Catalogue_SchoolYearKey == SchoolYearKey
                              select se_sj_cr.Training_Class_RoomKey).Contains(cr.Training_Class_RoomKey ) &&
                           cr.Status == true &&
                           cr.Capacity >= NumStudent
                         orderby
                           cr.Capacity,
                           cr.Projector descending,
                           cr.Floor,
                           cr.Range
                         select new
                         {
                             cr.Training_Class_RoomKey,
                             cr.ID,
                             cr.Name,
                             cr.Capacity,
                             cr.Floor,
                             cr.Range,
                             cr.Projector,
                             cr.Status,
                             cr.Note
                         };
            return IQuery;
        }

        #region [Chi]
        public static IQueryable GetClassRoomFree(
            this Table<Training_Class_Room> nClassRoom,
            Table<Training_SubjectOpenRequest> nTraining_SubjectOpenRequest,
            int _iDayOfWeek,
            int _iSession)
        {
            var roomBuSy = nTraining_SubjectOpenRequest.Where(
                    p => p.Startus == 1 && p.Training_WeekDayKey == _iDayOfWeek &&
                        p.Training_SessionKey == _iSession).Select(p => p.Training_Class_RoomKey);
            var ListRoomFree = from room in nClassRoom
                               where !roomBuSy.Contains(room.Training_Class_RoomKey)
                               select room;
            return ListRoomFree;
        }
        #endregion

        #region [An]
        /// <summary>
        /// Lấy mã, tên phòng
        /// </summary>
        /// <param name="nTraining_Class_Room"></param>
        /// <returns></returns>

        public static IQueryable GetIDNameRoom(this Table<Training_Class_Room> nTraining_Class_Room)
        {
            var values = from t in nTraining_Class_Room
                         orderby
                            t.ID
                         select new
                         {
                             t.Training_Class_RoomKey,
                             t.ID,
                             t.Name
                         };
            return values.Distinct();
        }

        /// <summary>
        /// Lấy danh sách phòng đăng ký dạy bù (có buổi giảng hoặc không)
        /// </summary>
        /// <param name="nTraining_Class_Room"></param>
        /// <param name="nTraining_Session_Subject_Class_Room"></param>
        /// <param name="m_iSession"></param>
        /// <returns></returns>

        // Còn thiếu điều kiện năm học, học kỳ cho danh sách phòng
        public static IQueryable GetDataWithSession(this Table<Training_Class_Room> nTraining_Class_Room, Table<Training_Session_Subject_Class_Room> nTraining_Session_Subject_Class_Room, int m_iSession)
        {
            var values = from t in nTraining_Class_Room
                         orderby t.ID
                         select new
                         {
                             t.Training_Class_RoomKey,
                             t.ID,
                             //t.Name,
                             t.Capacity,
                             Floor = t.Floor == null ? 0 : t.Floor,
                             Range = t.Range == null ? "-" : t.Range,
                             Projector = t.Projector == true ? "Có" : "Không",
                             t.Status,
                             t.Note
                         };
            if (m_iSession > 0)
            {
                var condition = from t1 in nTraining_Session_Subject_Class_Room

                                // Còn thiếu điều kiện năm học, học kỳ cho danh sách phòng

                                where t1.Training_SessionKey == m_iSession
                                select new
                                {
                                    t1.Training_Class_RoomKey
                                };
                var result = from t in values
                             where
                             !(from t1 in condition
                               select new { t1.Training_Class_RoomKey }).Contains(new { t.Training_Class_RoomKey })
                             select t;
                return result.Distinct();
            }
            return values.Distinct();
        }
        #endregion

        #region [Hữu]
        /// <summary>
        /// Lọc danh sách phòng học theo sỉ số/máy chiếu
        /// </summary>
        /// <param name="_iCapacity">Sỉ số</param>
        /// <param name="_iProjector">Máy chiếu : 0-> ko máy chiếu, 1-> có máy chiếu, -1-> bỏ qua xét điều kiện máy chiếu</param>
        /// <returns></returns>
        public static IQueryable Filter(this Table<Training_Class_Room> class_Room, int _iCapacity = -1, int _iProjector=-1)
        {
            var q = from clR in class_Room where clR.Status==true select clR;
            if (_iProjector != -1)
            {
                q=q.Where(p=>p.Projector== bool.Parse(_iProjector.ToString()));
            }
            if(_iCapacity >0)
            {
                q=q.Where(p=>p.Capacity >= _iCapacity);
            }
            return q.OrderBy(p=>p.Capacity);
        }
        #endregion
    }
}
